package com.docjohnson.pamphlet.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.View.OnClickListener;

import com.docjohnson.pamphlet.R;
import com.docjohnson.pamphlet.utils.Constants;

public class Dialogs implements Constants {

    private static ProgressDialog progressDialog;
    private Dialog dialog = null;

    public static void showProgressDialog(final Activity activity){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog = new ProgressDialog(activity);
                progressDialog.setMessage("Loading...");
                progressDialog.setCancelable(false);
                progressDialog.show();
            }
        });
    }
    
    public static void setProgressDialogMessage(final Activity activity, final String message) {
	    activity.runOnUiThread(new Runnable() {
	        @Override
	        public void run() {
	        	if (progressDialog.isShowing()) {
	        		progressDialog.setMessage(message);
	        	}
	        }
	    });
    }

    public static void closeProgressDialog(final Activity activity){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(progressDialog != null && progressDialog.isShowing()){
                    progressDialog.dismiss();
                }
            }
        });
    }

    public static void showHorizontalProgressDialog(final Activity activity){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog = new ProgressDialog(activity);
                progressDialog.setMessage("Loading...");
                progressDialog.setCancelable(false);
                progressDialog.show();
            }
        });
    }

    public static void closeHorizontalProgressDialog(final Activity activity){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(progressDialog != null && progressDialog.isShowing()){
                    progressDialog.dismiss();
                }
            }
        });
    }

    public static Dialog showNoNetworkDialog(Activity activity, OnClickListener onClickListener, OnClickListener onClickListener1, OnClickListener onClickListener2) {
        Dialog dialog = new Dialog(activity);
        dialog.setTitle("Information");
        dialog.setContentView(R.layout.no_internet_connection);
        dialog.findViewById(R.id.retry).setOnClickListener(onClickListener);
        dialog.findViewById(R.id.settings).setOnClickListener(onClickListener1);
        dialog.findViewById(R.id.cancel).setOnClickListener(onClickListener2);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }

    public static void showErrorDialog(final Activity activity, final String errorString) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new AlertDialog.Builder(activity).setTitle("Error").setMessage(errorString).setNeutralButton("OK", null).create().show();
            }
        });
    }
}